<?php
/**
 * Author:xsc
 * Time:2023/10/19 17:22
 */

namespace App\Http\Controllers\Common;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\Extensions\Coupon\CouponService;
use App\Repository\SpecialPrivilegeUgcOrderRepo;
use App\Repository\SpecialPrivilegeOrderRepo;

class CallbackController
{

    /**
     * 赋能自动领取,回调
     * @param Request $request
     * @return void
     * Author:xsc
     * Time:2023/10/25 15:01
     */
    public function privilegeAutoReceiveCallback(Request $request)
    {
        $data = $request->input();

        Log::info('[赋能自动领取回调]参数:' . json_encode($data, JSON_UNESCAPED_UNICODE));

        try {
            // 卡券
            $coupon = CouponService::getInstance();

            if (!isset($data['sign'])) {
                throw new \Exception('缺少签名');
            }

            $check = $coupon->decrypt($data['sign']);
            if (!$check) {
                throw new \Exception('签名验证未通过');
            }

            if (!isset($data['data']['orderNo']) || !isset($data['data']['status'])) {
                throw new \Exception('缺少必要字段');
            }

            // 赋能订单
            $order_info = SpecialPrivilegeOrderRepo::getInstance()->detail(['ordersn' => $data['data']['orderNo']]);
            if (!$order_info) {
                throw new \Exception('订单信息不存在');
            }

            $status = $data['data']['status'];
            $update = [];

            if ($status == 1) {
                // 充值中
                $update['status'] = 2;
            } else if ($status == 2) {
                // 失败
                $update['status'] = 3;
            } else if ($status == 3) {
                // 成功
                $update['status'] = 1;
                $update['send_time'] = date('Y-m-d H:i:s');

                $update['card_id'] = $data['data']['couponList'][0]['cardNo'] ?: '';
                $update['woff_code'] = $data['data']['couponList'][0]['msgCode'] ?: '';
            }

            $res = SpecialPrivilegeOrderRepo::getInstance()->save($update, $order_info['id']);
            if (!$res) {
                throw new \Exception('订单信息更新失败');
            }

            $this->success();
        } catch (\Exception $e) {
            Log::info('[赋能自动领取回调]失败:' . $e->getMessage());
            $this->fail('2002', $e->getMessage());
        }
    }

    /**
     * ugc自动领取,领取红包的回调
     * @param Request $request
     * @return void
     * Author:xsc
     * Time:2023/10/20 10:22
     */
    public function ugcAutoReceiveCallback(Request $request)
    {
        $data = $request->input();

        Log::info('[ugc自动领取回调]参数:' . json_encode($data, JSON_UNESCAPED_UNICODE));

        try {
            // 卡券
            $coupon = CouponService::getInstance();

            if (!isset($data['sign'])) {
                throw new \Exception('缺少签名');
            }

            $check = $coupon->decrypt($data['sign']);
            if (!$check) {
                throw new \Exception('签名验证未通过');
            }

            if (!isset($data['data']['orderNo']) || !isset($data['data']['status'])) {
                throw new \Exception('缺少必要字段');
            }

            // ugc赋能订单
            $order_info = SpecialPrivilegeUgcOrderRepo::getInstance()->getByOrderSn($data['data']['orderNo']);
            if (!$order_info) {
                throw new \Exception('订单信息不存在');
            }

            $status = $data['data']['status'];
            $update = [];

//            if ($status == 1) {
//                // 充值中
//                $update['status'] = 3;
//            } else if ($status == 2) {
//                // 失败
//                $update['status'] = 1;
//            } else if ($status == 3) {
//                // 成功
//                $update['status'] = 2;
//                $update['receive_time'] = date('Y-m-d H:i:s');
//            }

            if ($status == 3) {
                // 成功
                $update['status'] = 2;
                $update['receive_time'] = date('Y-m-d H:i:s');

                $update['card_id'] = $data['data']['couponList'][0]['cardNo'] ?: '';
                $update['woff_code'] = $data['data']['couponList'][0]['msgCode'] ?: '';
            }
            if ($update) {
                $res = SpecialPrivilegeUgcOrderRepo::getInstance()->save($update, $order_info['id']);
                if (!$res) {
                    throw new \Exception('订单信息更新失败');
                }
            }

            $this->success();
        } catch (\Exception $e) {
            Log::info('[ugc自动领取回调]失败:' . $e->getMessage());
            $this->fail('2002', $e->getMessage());
        }
    }

    private function success()
    {
        $resp = [
            'code'    => '0000',
            'msg'     => '提交成功',
            'sign'    => '',
            'success' => true
        ];
        echo json_encode($resp, 256);
        die;
    }

    private function fail(string $code, string $msg)
    {
        $resp = [
            'code'    => $code,
            'msg'     => $msg,
            'sign'    => '',
            'success' => true
        ];
        echo json_encode($resp, 256);
        die;
    }
}
